#!/usr/bin/env python

import rospy
import os
from task_planner.msg import Environment
from task_planner.msg import EnvUpdate
modifications = None

i = 0


############################## CLASS ACTIONS ############################
class Actions:

	# class attributes	
	a_TurnOnLight = False
	a_TurnOffLight = True	
	a_TurnOnBlinds = False		
	a_Auto2Manual = False
	a_Manual2Auto = True
		
	# print method	
	def __str__(self): 
		a0 = '\n # ACTIONS STATUS: \n' 
		a1 = '  - a_TurnOnLight =  ' + str(self.a_TurnOnLight) + '\n'
		a2 = '  - a_TurnOffLight = ' + str(self.a_TurnOffLight) + '\n'
		a3 = '  - a_TurnOnBlinds = ' + str(self.a_TurnOnBlinds) + '\n'
		a4 = '  - a_Auto2Manual = ' + str(self.a_Auto2Manual) + '\n'
		a5 = '  - a_Manual2Auto = ' + str(self.a_Manual2Auto) + '\n'		
		return a0+a1+a2+a3+a4+a5
	
	# Switch on the blinds
	def TurnOnBlinds(self,env):
		if (env.p_BlindsON == False) and (env.p_Presence == True):
			self.a_TurnOnBlinds = True
			
			msg = EnvUpdate()
			msg.set_BlindsON = True
			msg.p_BlindsON 	 = True
			
			msg.set_LightON 	 = False
			msg.set_AutoMode 	 = False
			msg.set_SunLight 	 = False
			msg.set_LightButton  = False
			msg.set_BlindsButton = False
			modifications.publish(msg)
			
	# Switch on the light
	def TurnOnLight(self,env):
		if (env.p_LightON == False) and (env.p_Presence == True):
			self.a_TurnOffLight = False
			
			msg = EnvUpdate()
			
			msg.set_LightON 	 = True
			msg.p_LightON 		 = True
			
			msg.set_BlindsON	 = False
			msg.set_AutoMode 	 = False
			msg.set_SunLight 	 = False
			msg.set_LightButton  = False
			msg.set_BlindsButton = False
			
			self.a_TurnOnLight  = True
			self.a_TurnOffLight = False
			modifications.publish(msg)
							
	# Switch off the light
	def TurnOffLight(self,env):
		if (env.p_LightON == True) and (env.p_Presence == False):
			self.a_TurnOffLight = True
			self.a_TurnOnLight  = False
			
			msg = EnvUpdate()
			
			msg.set_LightON 	 = True
			msg.p_LightON 		 = False
			
			msg.set_BlindsON	 = False
			msg.set_AutoMode 	 = False
			msg.set_SunLight 	 = False
			msg.set_LightButton  = False
			msg.set_BlindsButton = False
			modifications.publish(msg)
				
	# Change in Automatic mode
	def Manual2Auto(self,env):
		if (env.p_Presence == False):
			self.a_Manual2Auto = True
			self.a_Auto2Manual = False
			
			msg = EnvUpdate()
			msg.set_AutoMode = True
			msg.p_AutoMode 	 = True
			
			msg.set_LightON 	 = False		
			msg.set_BlindsON	 = False
			msg.set_AutoMode 	 = False
			msg.set_SunLight 	 = False
			msg.set_LightButton  = False
			msg.set_BlindsButton = False
			modifications.publish(msg)
				
	# Change Manual mode
	def Auto2Manual(self,env):
		if (env.p_AutoMode == True) and ((env.p_LightButton == True) or (env.p_BlindsButton == True)):
			self.a_Auto2Manual = True
			self.a_Manual2Auto = False
			
			msg = EnvUpdate()
			msg.set_AutoMode = True
			msg.p_AutoMode 	 = False
			
			msg.set_LightON 	 = False		
			msg.set_BlindsON	 = False
			msg.set_AutoMode 	 = False
			msg.set_SunLight 	 = False
			msg.set_LightButton  = True
			msg.p_LightButton = False
			msg.set_BlindsButton = True
			msg.p_BlindsButton = False
			modifications.publish(msg)
			
################################### CALLBACK ###################################
def callback(env):
	global i
	i = i+1
	
	print "+++++++++++++++++ MANAGE %d +++++++++++++++++ " % i
	
	print " --------------Message---------------- "
	print env
	print act
	print " ------------------------------------- "

	# Transition T8
	if (act.a_Manual2Auto == True) and (env.p_Presence == True) and (env.p_SunLight == True):
		print "8"
		act.TurnOnBlinds(env)
		act.a_Manual2Auto = False
		print "T8"

	# Transition T7
	elif (act.a_TurnOnLight == True) and (env.p_Presence == False):
		print "7"
		act.TurnOffLight(env)
		act.Manual2Auto(env)
		print "T7"

	# Transition T6
	elif (act.a_TurnOnLight == True) and (env.p_LightButton == True):
		print "6"
		act.Auto2Manual(env)
		env.p_LightButton = False
		print "T6"

	# Transition T5
	elif (act.a_TurnOnLight == True) and (env.p_BlindsButton == True):
		print "5"
		act.Auto2Manual(env)
		env.p_BlindsButton = False
		print "T5"

	# Transition T4 
	elif (act.a_Manual2Auto == True) and (env.p_Presence == True) and (env.p_SunLight == False):
		print "4"
		act.TurnOnLight(env)
		act.a_Manual2Auto = False
		print "T4"

	# Transition T3
	elif (act.a_Auto2Manual == True) and (env.p_Presence == False):
		print "3"
		act.Manual2Auto(env)
		print "3B"
		act.TurnOffLight(env)
		print "T3"

	# Transition T2 
	elif (act.a_TurnOnBlinds == True) and (env.p_BlindsButton == True):
		print "2"
		act.Auto2Manual(env)
		env.p_BlindsButton == False
		print "T2"

	# Transition T1
	elif (act.a_TurnOnBlinds == True) and (env.p_LightButton == True):
		print "1"
		act.Auto2Manual(env)
		env.p_LightButton = False
		print "T1"
 
   
def Manage():
	global modifications
	
	os.system("clear") 
	rospy.init_node('ManageLightBlinds', anonymous=True)
	print " + ManageLightBlinds: START "
	
	
	rospy.Subscriber('/environment/status', Environment, callback)
	modifications = rospy.Publisher('/environment/modifications', EnvUpdate, latch = True)

	rospy.spin()

act = Actions()
if __name__ == '__main__':

	Manage()

